using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Rhino.Mocks;

namespace MagickServer.Tests {
    public static class ControllerExtensions {
        public static void HandleUnknownAction(this Controller c, string actionName) {
            var method = c.GetType().GetMethod("HandleUnknownAction", BindingFlags.NonPublic | BindingFlags.Instance);
            method.Invoke(c, new[] {actionName});
        }

        public static void SetupMockContext(this Controller c) {
            var context = MockRepository.GenerateMock<HttpContextBase>();
            var request = MockRepository.GenerateMock<HttpRequestBase>();
            context.Expect(o => o.Request).Repeat.Any().Return(request);
            var response = MockRepository.GenerateMock<HttpResponseBase>();
            context.Expect(o => o.Response).Repeat.Any().Return(response);
            c.ControllerContext = new ControllerContext(context, new RouteData(), c);
        }

        public static void SetupQueryString(this Controller c, NameValueCollection queryString) {
            c.ControllerContext.HttpContext.Request
                .Expect(o => o.QueryString)
                .Repeat.Any()
                .Return(queryString);
        }

        public static void SetupOutputStream(this Controller c, Stream s) {
            c.ControllerContext.HttpContext.Response
                .Expect(o => o.OutputStream)
                .Repeat.Any()
                .Return(s);
        }
    }
}